<?php
namespace Adminx\Controller;

class MemberController extends AdminController {

	#列表
	public function index(){ 
        $company = I('company');  
        $mobile = I('mobile');  

        if ($company!='') {
            $map['company'] = array('like','%'.$company.'%');
        }

        if ($mobile!='') {
            $map['mobile'] = $mobile;
        }

     
        $obj = M('Member');
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 15);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        foreach ($list as $key => $value) {
            $list[$key]['money'] = $this->getUserMoney($value['id']);
        }
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }

    #编辑
    public function edit(){
        if($_POST){
            $obj = D('Member'); //创建对象
            if ($data = $obj->create()) {//创建数据
                if ($data['password']=="") {
                    unset($data['password']);
                }else{
                    $data['password'] = md5($data['password']);
                }
                if ($obj->save($data)) { //保存到数据库
                    $this->success('操作成功',U('Member/index'));
                } else {
                    $this->error('编辑失败');
                }
            } else {
                $this->error($obj->getError());
            }
        }else{
            $id = (int) I('get.id');
            if (!isset ($id)) {
                $this->error('参数错误');
            }
            $obj = M('Member');
            $list = $obj->where('id=' . $id)->find();
            $this->assign('list', $list);
            $obj = M('Role');
            $this->assign('group',$obj->select());
            $this->display();
        }

    }

    public function go(){
        $id = I('get.id');
        if ($id=='' || !is_numeric($id)) {
            $this->error('参数错误');
        }
        $user = M('Member')->where(array('id'=>$id,'disable'=>0))->find();
        if ($user) {
            $crypt = new \Think\Crypt;
            $cryptStr = $user['id'].','.get_client_ip();
            $flag = $crypt->encrypt($cryptStr,C('DATA_CRYPT_KEY'),0);
            session('userflag', $flag);
            $this->success('正在进入会员中心...', U('Home/Member/index'));
        }else{
            $this->error("用户不存在");
        }
    }

    //会员扣款
    public function pay(){
        if($_POST){
            $id = I('post.id');
            $msg = I('post.msg');
            $payMoney = I('post.money');

            if ($id=='' || !is_numeric($id)) {
                $this->error('参数错误');
            }

            if ($payMoney<=0) {
                $this->error('金额错误');
            }

            if ($msg=='') {
                $this->error('请输入扣款理由');
            }

            $user = M('Member')->where(array('id'=>$id))->find();      
            if (!$user) {
                $this->error('用户不存在');
            }else{
                $money = $this->getUserMoney($user['id']);
            }

            if ($money['money'] < $payMoney) {
                $this->error('用户余额不足，无法扣款');
            }

            //保存扣款日志
            $buyData = array(
                'type' => 3,
                'money' => $payMoney,
                'memberID' => $user['id'],
                'username' => $user['mobile'],
                'doID' => $this->admin['id'],
                'doUser' => $this->$admin['name'],
                'admin' => 1,
                'msg' => $msg,
                'createTime' => time()
                );
            $list = M('Finance')->add($buyData);
            if ($list) {
                $this->success('操作成功');
            }
        }else{
            $id = (int) I('get.id');
            if (!isset ($id)) {
                $this->error('参数错误');
            }
            $obj = M('Member');
            $list = $obj->where('id=' . $id)->find();
            if ($list) {
                $list['money'] = $this->getUserMoney($list['id']);
                $this->assign('list', $list);
                $this->display();
            }else{
                $this->error('会员不存在');
            }
            
        }
    }

    #删除
	public function del() {
		$this->all_del('member','reload');
        
	}
}
?>